package com.jic.order.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.RestResult;
import com.jic.order.base.BaseService;
import com.jic.order.entity.OrderAfterSaleTime;
import com.jic.order.entity.OrderParent;
import com.jic.order.enums.OrderStatusEnum;
import com.jic.order.mapper.OrderAfterSaleTimeMapper;
import com.jic.order.mapper.OrderParentMapper;
import com.jic.order.request.OrderAfterSaleTimeRequest;
import com.jic.order.response.OrderAfterSaleTimeResponse;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderAfterSaleTimeService;
import com.jic.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单售后有效期设置
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-17 02:38:00
 */
@Service
@Slf4j
public class OrderAfterSaleTimeServiceImpl extends BaseService implements OrderAfterSaleTimeService {

    @Autowired
    private OrderAfterSaleTimeMapper orderAfterSaleTimeMapper;

    @Autowired
    OrderParentMapper orderParentMapper;

    @Override
    public long insert(OrderAfterSaleTime orderAfterSaleTime) {
        long flag = orderAfterSaleTimeMapper.insert(orderAfterSaleTime);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return orderAfterSaleTimeMapper.delete(id);
    }

    @Override
    public long update(OrderAfterSaleTime orderAfterSaleTime) {
        return orderAfterSaleTimeMapper.update(orderAfterSaleTime);
    }

    @Override
    public OrderAfterSaleTime queryByPrimaryKey(Long id) {
        return orderAfterSaleTimeMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<OrderAfterSaleTime> query(OrderAfterSaleTime orderAfterSaleTime) {
        return orderAfterSaleTimeMapper.query(orderAfterSaleTime);
    }

    @Override
    public PageResult<OrderAfterSaleTimeResponse> queryPage(OrderAfterSaleTimeRequest orderAfterSaleTime, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<OrderAfterSaleTimeResponse> list = orderAfterSaleTimeMapper.queryPage(startIndex, pageQuery.getLimit(), orderAfterSaleTime);
        long count = orderAfterSaleTimeMapper.queryPageCount(orderAfterSaleTime);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 定时订单售后期
     *
     * @return {@link RestResult <String>}
     */
    @Override
    public RestResult<String> regularOrdersAfterSaleTimely() {
        log.info("订单售后期时任务开始===================start");
        List<OrderAfterSaleTime> orderAfterSaleTimes = orderAfterSaleTimeMapper.query(new OrderAfterSaleTime() {{
            setDeleteFlag(0);
        }});
        if (CollectionUtils.isNotEmpty(orderAfterSaleTimes)) {
            orderAfterSaleTimes.forEach(orderAfterSaleTime -> {
                //查询已签收并且在售后期的订单
                List<OrderParent> orderParentList = orderParentMapper.queryOrderInAfterSale();
                if (CollectionUtils.isNotEmpty(orderParentList)) {
                    //根据规则和时间判断订单是否在有效期
                    long day = orderAfterSaleTime.getAfterSaleDate();
                    Date now = new Date();
                    orderParentList.forEach(orderParent -> {
                        Date updateTime = orderParent.getUpdateTime();
                        Integer days = DateUtil.getDistanceDays(updateTime, now);
                        if (days > day) {
                            //置为失效
                            orderParent.setOrderStatus(OrderStatusEnum.FINISH.getCode());
                            orderParentMapper.update(orderParent);
                        }
                    });
                }
            });
        }
        log.info("订单售后期定时任务结束===================end");
        return null;
    }


}
